/**
 * 
 */
package org.acompanhamento.validator;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.acompanhamento.CoreAppMessages;
import org.acompanhamento.dao.StatusDAO;
import org.acompanhamento.domain.Status;
import org.acompanhamento.domain.StatusTransiction;
import org.acompanhamento.domain.Workflow;

import bancosys.tec.persist.validator.AbstractValidator;
import bancosys.tec.persist.validator.ValidationError;

/**
 * @author icaro
 *
 */
public class WorkflowValidator extends AbstractValidator<Workflow>{

	private StatusDAO statusDAO;

	@Override
	public List<ValidationError> validateInsert(Workflow bean) {
		ArrayList<ValidationError> errors = new ArrayList<ValidationError>();
		this.addError(errors, this.validateAllStatusHaveStatusTransiction(bean));
		return errors;
	}

	private ValidationError validateAllStatusHaveStatusTransiction(Workflow bean) {
		List<StatusTransiction> statusTransictions = bean.getStatusTransictions();
		List<Status> statuses = this.statusDAO.findByWorkflow(bean);
		Set<Status> statusWithTransiction = new HashSet<Status>();
		for (StatusTransiction statusTransiction : statusTransictions) {
			statusWithTransiction.add(statusTransiction.getFromStatus());
			statusWithTransiction.add(statusTransiction.getToStatus());
		}

		for (Status status : statuses) {
			if(!statusWithTransiction.contains(status)){
				return new ValidationError(CoreAppMessages.STATUS_WITH_OUT_TRANSICTION.create(status));
			}
		}
		return null;
	}

	public void setStatusDAO(StatusDAO statusDAO) {
		this.statusDAO = statusDAO;
	}
}
